package com.utils.toast

import android.R
import android.app.Activity
import android.app.Application
import android.content.Context
import android.graphics.PixelFormat
import android.os.Bundle
import android.view.WindowManager
import com.utils.ActivityUtils
import java.util.concurrent.atomic.AtomicBoolean

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 4/7/21
 */
class WindowToast(app: Application) : BaseToast() {

    private var windowManager:WindowManager? = null
    private val isShow = AtomicBoolean(false)
    override fun show() {
        //app 运行在前台
        toastView?.show()
        if (isShow.get()) {
            hide()
        }
        isShow.set(true)
        val params = WindowManager.LayoutParams()
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.width = WindowManager.LayoutParams.WRAP_CONTENT
        params.format = PixelFormat.TRANSLUCENT
        params.windowAnimations = R.style.Animation_Toast
        params.flags = (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        params.gravity = gravity
        params.x = xOffset
        params.y = yOffset
        try {
            val currentTopActivity = ActivityUtils.getCurrentTopActivity()
            if (currentTopActivity != null && !currentTopActivity.isFinishing) {
                windowManager = currentTopActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                windowManager?.addView(toastView?.getToastView(), params)
            }
            toastView?.getToastView()?.postDelayed(Runnable { hide() }, if (toastDuration == android.widget.Toast.LENGTH_SHORT) 1000 else 2000)
        } catch (e: Exception) {
            showSystemToast()
        }
    }

    private val lifecycleCallbacks = object : Application.ActivityLifecycleCallbacks{
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

        }

        override fun onActivityStarted(activity: Activity) {
        }

        override fun onActivityResumed(activity: Activity) {
        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        }

        override fun onActivityDestroyed(activity: Activity) {
        }

        override fun onActivityPrePaused(activity: Activity) {
            hide()
        }

    }

    private fun showSystemToast() {
        val systemToast = SystemToast()
        systemToast.setView(toastView)
        systemToast.show()
    }

    override fun hide() {
        if (!isShow.get()) {
            return
        }
        try {
            if (windowManager!=null) {
                toastView?.getToastView()?.let {
                    windowManager?.removeView(it)
                }
            }
        } catch (ignored: Exception) {
        }
        isShow.set(false)
    }

    init {
        app.registerActivityLifecycleCallbacks(lifecycleCallbacks)
    }
}
